/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.ViewInformation;
import org.skatiger.jrove.resource.Resource;
import org.skatiger.jrove.util.AssertUtil;

public class UIViewRoot extends UIComponentBase implements ViewInformation {
    public static final String UNIQUE_ID_PREFIX = "_jid";

    public AtomicLong uniqueIdCounter = new AtomicLong();

    private String _viewId;
    private String encoding;
    private List<Resource> resources;
    private _ComponentMap componentMap;

    @Override
    public String getId() {
        return _viewId;
    }

    /**
     * Construct an instance of the UIViewRoot.
     */
    private UIViewRoot() {
        componentMap = new _ComponentMap(this);
    }

    public Map<String, UIComponent> getComponentMap() {
        return componentMap;
    }

    public UIViewRoot(String viewId) {
        this();
        this._viewId = viewId;
    }

    public String getViewId() {
        return _viewId;
    }

    public void setViewId(String viewId) {
        this._viewId = viewId;
    }

    /**
     * Provides a unique id for this component instance.
     */
    public String createUniqueId() {
        return createUniqueId(getViewContext(), null);
    }

    public String createUniqueId(ViewContext context, String seed) {
        StringBuilder bld = __getSharedStringBuilder();
        long id = uniqueIdCounter.getAndIncrement();
        // Generate an identifier for a component. The identifier will be
        // prefixed with UNIQUE_ID_PREFIX, and will be unique within this
        // UIViewRoot.
        if (seed == null) {
            return bld.append(UNIQUE_ID_PREFIX).append(id).toString();
        }
        // Optionally, a unique seed value can be supplied by component creators
        // which should be included in the generated unique id.
        else {
            return bld.append(UNIQUE_ID_PREFIX).append(seed).toString();
        }
    }

    public List<Resource> getResources() {
        if (resources == null) {
            resources = new _ResourceList();
        }
        return resources;
    }

    public void addResource(Resource _resource) {
        AssertUtil.notNull(_resource);
        getResources().add(_resource);
    }

    public void addResources(List<Resource> _resources) {
        AssertUtil.notEmpty(_resources);
        for (Resource resource : _resources) {
            getResources().add(resource);
        }
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
}
